<?php
class HomeController extends CController{	
	private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
	public $mainMenu = "Karyawan";
	public $subMenu = "";
	public $title = "";		
	public $loginUser;		
	public function init() {
		parent::init();
		if (!LoginUser::isLogin("karyawan")){
			LoginUser::redirectToLoginPage("Karyawan");
		}else{
			$this->loginUser = LoginUser::getUser();
		}					
	}
	public function actionIndex(){
 		$this->layout ="//karyawan/layouts/ly_blank"; 
		$karyawan = $this->loginUser ->karyawan;		
		$karyawan_id=$karyawan->id;		
		$data=array("department_id"=>"","karyawan_id"=>$karyawan_id);			
		$this->render('index',$data);	

	}
	
	public function actiondetailList(){	 
		$karyawan = $this->loginUser ->karyawan;		
		$department_id = $karyawan->department_id;	
		$karyawan_id=$karyawan->id;			
		$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","");				
		$tingkatan_id = Yii::app()->request->getParam("tingkatan_id","");				
		$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");			
		$tahunAjarans = VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE and department_id=:departmentId   group by tahun_ajaran_id order by tahun_ajaran_id desc  ",array(":departmentId"=>$department_id));			
		if ($tahunAjarans == null){
			$tahun_ajaran_id == "";
		}else{
			$tahun_ajaran_id = $tahun_ajaran_id == "" ? $tahunAjarans[0]->tahun_ajaran_id : $tahun_ajaran_id;
		}			
	 
		$tingkatans =  VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE and department_id=:departmentId and tahun_ajaran_id=:tahun_ajaran_id  group by tingkatan_id order by id",array(":departmentId"=>$department_id,":tahun_ajaran_id"=>$tahun_ajaran_id));

		$kelastahunajaranid='';
		$mcon= Yii::app()->db;
if (count($tingkatans) > 0){
			foreach ($tingkatans as $rs_tingkatans){
				$rsKelas = VJadwalPelajaranKelasTahunAjaran::model()->findAll("tingkatan_id=:tingkatan_id and department_id=:departmentId and tahun_ajaran_id=:tahun_ajaran_id  and is_active = TRUE group by kelas_id,tingkatan_id order by id",array(":tingkatan_id"=>$rs_tingkatans["tingkatan_id"],":departmentId"=>$department_id,":tahun_ajaran_id"=>$tahun_ajaran_id));
				if (count($rsKelas) > 0){
					foreach ($rsKelas as $rs_Kelas){
						$kelastahunajaranid=$kelastahunajaranid.$rs_Kelas["kelas_tahun_ajaran_id"].',';
					}
				}
			}
		}
		$kelastahunajaranid = substr($kelastahunajaranid,0,-1);	
		$sqlall='select  C.id as id, C.create_date, C.parent_id, C.karyawan_id, C.student_id as student_id,subject,message,last_sender,read_status, B.sid, B.name, VSKTA.department_id,VSKTA.tahun_ajaran_id, VSKTA.kelas_tahun_ajaran_id
FROM  `m_commbook` C 
left join m_student B on C.student_id=B.id 
left join v_student_kelas_tahun_ajaran  VSKTA on VSKTA.student_id = C.student_id  
where  C.id is not null and parent_id=0  ';
		$sqltask_count = $sqlall .  " and  kelas_tahun_ajaran_id in (".$kelastahunajaranid.")   and C.karyawan_id=:karyawan_id  and read_status=1 and last_sender='parent' order by kelas_tahun_ajaran_id desc";
		$mReportMonthCommands=$mcon->createCommand($sqltask_count);		
		$mReportMonthCommands->bindValue('karyawan_id',$karyawan_id); 					
		$mReportMonthCommands=$mReportMonthCommands->queryAll();		
		$this->result["actionStatus"] =0;
		$this->result["actionMessage"] ='';
		$this->result["actionError"] =array();
		$this->result["model"] = $mReportMonthCommands;			
		header('Content-type:application/json');
		echo CJSON::encode($this->result);		
	}	
	
		public function actionedit(){		
			$op = Yii::app()->request->getParam("op","add");
			$status = Yii::app()->request->getParam("is_active",0) === "on"?1:0 ;			
			if ($op == "add"){
				LoginUser::getPermission("karyawan_commbook_add");
				//print_r ($_POST);
				//echo "DATA SUDAH DI ADD........";
				$commbook = new MCommbook;
				$commbook->create_date = date("Y-m-d H:i:s");
				$commbook->update_date = date("Y-m-d H:i:s");					
				$commbook->parent_id = 0; 
				$commbook->karyawan_id = Yii::app()->request->getParam("karyawan_id",""); 
				$commbook->student_id = Yii::app()->request->getParam("student_id","");
				$commbook->subject = Yii::app()->request->getParam("subject","");
				$commbook->message = Yii::app()->request->getParam("message","");
				$commbook->last_sender = "Teacher";
				$commbook->read_status = 1;				 				
				if ($commbook->save()){
					echo "<script>
						alert('".Yii::t("dialog","Data sudah di tambahkan...!"). "');					
						window.close();					
					</script>";
					exit;
				}else{
					print_r($commbook->getErrors());
				}				
			}else if ($op == "edit"){
				LoginUser::getPermission("karyawan_commbook_edit");
				//print_r ($_POST);
				$id = Yii::app()->request->getParam("id","");	
				$commbook_update = MCommbook::model()->find("id=:id",array(":id"=>$id));							
				$commbook_update->update_date = date("Y-m-d H:i:s");
				$commbook_update->last_sender = "Teacher";
				$commbook_update->read_status = 1;				 				
				if ($commbook_update->save()){
					$commbook = new MCommbook;
					$commbook->create_date = date("Y-m-d H:i:s");
					$commbook->update_date = date("Y-m-d H:i:s");					
					$commbook->parent_id = $id; 
					$commbook->karyawan_id = Yii::app()->request->getParam("karyawan_id",""); 
					$commbook->student_id = Yii::app()->request->getParam("student_id","");
					$commbook->subject = "Re: ".Yii::app()->request->getParam("subject","");
					$commbook->message = Yii::app()->request->getParam("message","");
					$commbook->last_sender = "Teacher";
					$commbook->read_status = 1;				 				
					if ($commbook->save()){
						echo "<script>
							alert('".Yii::t("dialog","Data sudah di tambahkan...!"). "');					
							window.close();					
						</script>";
						exit;
					}else{
						print_r($commbook->getErrors());
					}			
				}	
			}else if ($op == "del"){
				if (!LoginUser::getPermission("karyawan_commbook_del",null,false)){
					$this->result["actionStatus"] =1;
					$this->result["actionMessage"] = Yii::t("alert","Anda tidak memiliki ijin untuk menghapus data ini !");
					$this->result["actionError"] =array();
					$this->result["model"] = array();
					header('Content-type:application/json');
					echo CJSON::encode($this->result);
					return;
				}
				$id = Yii::app()->request->getParam("id","");
				$conn = Yii::app()->db;
				$sql = "delete from m_semester where id = :id";
				$command = $conn->createCommand($sql);
				try{
					//$command->bindParam(":id",$id,PDO::PARAM_INT);
					if ($command->execute(array(":id"=>$id))){
						$this->result["actionStatus"] =0;
						$this->result["actionMessage"] ="";
						$this->result["actionError"] =array();
						$this->result["model"] = array();
					}
				}catch(Exception $e){
					$this->result["actionStatus"] =2;
					$this->result["actionMessage"] = $e->getMessage();
					$this->result["actionError"] =array();
					$this->result["model"] = array();
				}
				Yii::app()->db->active = false;
			}
			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
		}

	public function actionform(){
			$op = Yii::app()->request->getParam("op","add");
			$callback = Yii::app()->request->getParam("callback","");
			$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","0");
			$karyawan_id = Yii::app()->request->getParam("karyawan_id","0");			
			$student_id = Yii::app()->request->getParam("student_id","0");	
			$student_sid = Yii::app()->request->getParam("student_sid","");	
 					
			$students =VStudentKelasTahunAjaran::model()->findAll(array("condition"=>"is_active = TRUE and kelas_tahun_ajaran_id = $kelas_tahun_ajaran_id","order"=>"sid"));		
			if ($students == null){
				$student_id == "";
				$student_sid = "";	
			}else{
				$student_id = $student_id == "" ? $students[0]->student_id : $student_id;
				$student_sid =  $student_sid == "" ? $students[0]->sid : $student_sid;			
			}							
					 
			$data = array("op"=>"add","callback"=>"","id"=>0,"students"=>$students,"student_id"=>$student_id,"student_sid"=>$student_sid,"kelas_tahun_ajaran_id" =>$kelas_tahun_ajaran_id,"karyawan_id" =>$karyawan_id,"subject"=>"","message"=>"","is_active"=>"1","data_view"=>"");
			
			if ($op == "add"){
				LoginUser::getPermission("karyawan_commbook_add");
				$data["op"] = "add";
				$data["callback"] = $callback;
			}else if($op =="edit"){				
				LoginUser::getPermission("karyawan_commbook_edit");			
				$data["op"] = "edit";
				$data["callback"] = $callback;								
				$id = Yii::app()->request->getParam("id","0");				
				$data["id"] = $id;
				$data["data_view"]="<table class=message><tr><Td>From</td><td>Message</td></tr>";
				$rsTingkat = MCommbook::model()->findAll(array("condition"=>"id = $id","order"=>"id"));
				if (count($rsTingkat) > 0){
				$data["karyawan_id"] = $karyawan_id;				
				$data["subject"] = $rsTingkat[0]->subject;
				$data["student_id"] = $rsTingkat[0]->student_id;
				$student_id = $rsTingkat[0]->student_id;				
 				$last_sender_cek= $rsTingkat[0]->last_sender;
				$last_subject_cek= $rsTingkat[0]->subject;				
				$data["data_view"]=$data["data_view"]."<tr><td width=50  >".  date_format(date_create($rsTingkat[0]->create_date), 'd-M-Y').'<br>'.$rsTingkat[0]->last_sender."</td><td valign=top><b>".$rsTingkat[0]->subject."</b><br>".$rsTingkat[0]->message."</td></tr>";
				
				$students =VStudentKelasTahunAjaran::model()->findAll(array("condition"=>"is_active = TRUE and student_id = $student_id","order"=>"sid"));		
				if ($students == null){
					$student_sid = "";	
				}else{
					$student_sid =  $student_sid == "" ? $students[0]->sid : $student_sid;			
				}		
				$data["student_sid"] =$student_sid; 									
				}	
				
				$rsTingkat = MCommbook::model()->findAll(array("condition"=>"parent_id = $id","order"=>"id"));
				if (count($rsTingkat) > 0){
					foreach ($rsTingkat as $rs_Tingkat){
						$data["data_view"]=$data["data_view"]."<tr><td width=50 >".date_format(date_create($rs_Tingkat["update_date"]), 'd-M-Y').'<br>'.$rs_Tingkat["last_sender"]."</td><td valign=top>".$rs_Tingkat["message"]."</td></tr>";
						$last_id_cek= $rs_Tingkat["id"];
					}
				}								
 				$data["data_view"]=$data["data_view"]."</table>";
					
				if ($last_sender_cek=="Parent"){	
					$commbook_update = MCommbook::model()->find("id=:id",array(":id"=>$id));							
					$commbook_update->update_date = date("Y-m-d H:i:s");
					$commbook_update->read_status = 0;		
					$commbook_update->subject = $last_subject_cek;									 				
						if ($commbook_update->save()){
							/*echo "<script>
								alert('".Yii::t("dialog","Data sudah di tambahkan...!"). "');					
								window.close();					
							</script>";
							exit;*/
						}else{
							print_r($commbook_update->getErrors());
						}	 
					$commbook_update = MCommbook::model()->find("id=:id",array(":id"=>$last_id_cek));							
					$commbook_update->update_date = date("Y-m-d H:i:s");
					$commbook_update->read_status = 0;		
					$commbook_update->subject = $last_subject_cek;									 				
					if ($commbook_update->save()){
						/*echo "<script>
							alert('".Yii::t("dialog","Data sudah di tambahkan...!"). "');					
							window.close();					
						</script>";
						exit;*/
					}else{
						print_r($commbook_update->getErrors());
					}	 					
				}
//				echo $data["data_view"];
//				exit;
				
			}			
			$this->layout ="//layouts/ly_blank";
			$this->render("form",$data);
		}
		
	protected function beforeRender($view){
		if ($this->subMenu != ""){
			$this->title = $this->mainMenu . " - " .$this->subMenu;
		}
		return true;
	}
		
	public function actionPopupWindow()
	{
		$this->layout = "//layouts/ly_blank";
		$this->render('popupWindow');
	}		
		
	}
?>